using Game.Core;
using Game.Main.Event;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using System.Diagnostics;

namespace Game.Main.Controller
{
	public class SceneController : BaseController
	{
		private SceneModel model;

		private Scene scene;

		private MainView mainView;

		private GameView gameView;

		private MapView mapView;

		private SplashView splashView;

		private LoadingView loadingView;

		//private ValidationView validationView;

		//private PurchaseView purchaseView;

		private CameraManager cameraManager;

		private SaveManager saveManager;

		private UIManager uiManager;

		private SceneType sceneType;

		public SceneController()
		{
			model = Singleton<SceneModel>.Instance;
			scene = Singleton<Scene>.Instance;
			scene.SetController(this);
			cameraManager = Singleton<CameraManager>.Instance;
			cameraManager.Bind("game.camera_pos_change", OnCameraPosChange);
			saveManager = Singleton<SaveManager>.Instance;
			if (!saveManager.gameVersion.Equals("1.0"))
			{
				saveManager.gameVersion = "1.0";
				ClearArchive();
			}
			uiManager = Singleton<UIManager>.Instance;
            //OCSplashView(true);
            ChangeScene(SceneType.MAIN_VIEW);
        }

		protected override void BindGlobalEvents()
		{
			GlobalEventSystem.Bind("game.change_scene_by_elevator", OnChangeSceneByElevator);
			GlobalEventSystem.Bind("game.change_scene_by_ambulance", OnChangeSceneByAmbulance);
			GlobalEventSystem.Bind("game.change_brush", OnChangeBrush);
			GlobalEventSystem.Bind("game.open_close_lamp", OnOpenCloseLamp);
			GlobalEventSystem.Bind("game.change_sun_light", OnSunLightChange);
			GlobalEventSystem.Bind("game.change_sun_state", OnSunStateChange);
			GlobalEventSystem.Bind("game.remove_item", OnRemoveItem);
			GlobalEventSystem.Bind("game.can_touch_scene", OnCanTouchScene);
			GlobalEventSystem.Bind("game.can_touch_ui", OnCanTouchUI);
			GlobalEventSystem.Bind("game.purchasing_result", OnPurchasingResult);
		}

		private void OnPurchasingResult(BaseEvent evt)
		{
			if ((bool)evt.Parameters)
			{
				Singleton<SaveManager>.Instance.IsUnlock = true;
				if (scene != null)
				{
					scene.UnlockScene();
				}
				SetLockMaskActive(false);
				//OCPurchaseView(false, false);
				//OCValidationView(false, false);
			}
		}

		public void OnSplashViewClose()
		{
			OCSplashView(false);
			ChangeScene(SceneType.MAIN_VIEW);
			model.LoadArchiveData();
		}

		public void ChangeScene(SceneType type)
		{
			//MyAdManager.Instance.MyShowStartBox(() =>
			//{

			//});
			sceneType = type;
			if (scene != null)
			{
				scene.CanTouch(false);
                //scene.CanTouch(true);
                scene.ReleaseAllTouch();
			}
			if (loadingView == null)
			{
				loadingView = new LoadingView(this);
				loadingView.Bind("game.loading_view_appear", OnLoadingViewAppear);
			}
			loadingView.IsAppear(true, 1f);
			loadingView.Open();
		}

		private void OnLoadingViewAppear(BaseEvent evt)
		{
			scene.SaveMapArchive();
			scene.StopGame();
			scene.ClearScene();
			OCMainView(false);
			OCMapView(false);
			OCGameView(false);
			//OCPurchaseView(false, true);
			//OCValidationView(false);
			if (sceneType == SceneType.MAIN_VIEW)
			{
				OCSplashView(false);
				OCMainView(true);
			}
			else if (sceneType == SceneType.MAP_VIEW)
			{
				OCMapView(true);
			}
			else if (sceneType == SceneType.SCENE_1)
			{
                MyAdManager.Instance.MyShowNative();
                OCGameView(true);
                //OCGameView(false);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[0]);
                //mapView.Destroy();
            }
			else if (sceneType == SceneType.SCENE_2)
			{
                MyAdManager.Instance.MyShowNative();
                //OCGameView(true);
                OCGameView(true);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[1]);
                //mapView.Destroy();
            }
			else if (sceneType == SceneType.SCENE_3)
			{
                MyAdManager.Instance.MyShowNative();
                //OCGameView(true);
                OCGameView(true);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[2]);
                //mapView.Destroy();
            }
			else if (sceneType == SceneType.SCENE_4)
			{
                MyAdManager.Instance.MyShowNative();
                //OCGameView(true);
                OCGameView(true);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[3]);
                //mapView.Destroy();
            }
			else if (sceneType == SceneType.SCENE_5)
			{
                MyAdManager.Instance.MyShowNative();
                //OCGameView(true);
                OCGameView(true);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[4]);
                //mapView.Destroy(); mapView.Destroy();
            }
			else if (sceneType == SceneType.SCENE_6)
			{
                MyAdManager.Instance.MyShowNative();
                //OCGameView(true);
                OCGameView(true);
                //OCMapView(true);
                scene.InitMap(model.mapConfig[5]);
                //mapView.Destroy();
            }
		}

		public void OnSceneInitComplete(BaseEvent evt)
		{
			if (loadingView != null)
			{
				loadingView.CloseView();
			}
			if (gameView != null)
			{
				//gameView.SetMaskActive(false);
			}
		}

		private void OCSplashView(bool val)
		{
			if (val)
			{
				if (splashView == null)
				{
					splashView = new SplashView(this);
				}
				splashView.Open();
			}
			else if (splashView != null)
			{
				splashView.Destroy();
				splashView = null;
			}
		}

		private void OCMainView(bool val)
		{
			if (val)
			{
				if (mainView == null)
				{
					mainView = new MainView(this);
					mainView.Bind(ViewEvent.VIEW_OPEN, OnSceneInitComplete);
				}
				mainView.Open();
			}
			else if (mainView != null)
			{
				mainView.Destroy();
				mainView = null;
			}
		}

		private void OCMapView(bool val)
		{
			if (val)
			{
				if (mapView == null)
				{
					mapView = new MapView(this);
					mapView.Bind(ViewEvent.VIEW_OPEN, OnSceneInitComplete);
				}
				mapView.Open();
			}
			else if (mapView != null)
			{
				mapView.Destroy();
				mapView = null;
			}
		}

		public void OCValidationView(bool val, bool isDestory = true, int type = 0)
		{
			//if (val)
			//{
			//	if (validationView == null)
			//	{
			//		validationView = new ValidationView(this);
			//		validationView.Bind(ViewEvent.VIEW_OPEN, OnSceneInitComplete);
			//	}
			//	validationView.validationType = type;
			//	validationView.Open();
			//}
			//else if (validationView != null)
			//{
			//	if (isDestory)
			//	{
			//		validationView.Destroy();
			//	}
			//	else
			//	{
			//		validationView.Close();
			//	}
			//	validationView = null;
			//}
		}

		public void OCPurchaseView(bool val, bool isDestory)
		{
			//if (val)
			//{
			//	if (purchaseView == null)
			//	{
			//		purchaseView = new PurchaseView(this);
			//		purchaseView.Bind(ViewEvent.VIEW_OPEN, OnSceneInitComplete);
			//	}
			//	purchaseView.Open();
			//}
			//else if (purchaseView != null)
			//{
			//	if (isDestory)
			//	{
			//		purchaseView.Destroy();
			//	}
			//	else
			//	{
			//		purchaseView.Close();
			//	}
			//	purchaseView = null;
			//}
		}

		private void OCGameView(bool val)
		{
			if (val)
			{
				if (gameView == null)
				{
					gameView = new GameView(this);
				}
				gameView.Open();
			}
			else if (gameView != null)
			{
				gameView.Destroy();
				gameView = null;
			}
		}

		public void SetLockMaskActive(bool val)
		{
			if (gameView != null)
			{
				gameView.SetLockMaskActive(val);
			}
		}

		public void Update()
		{
			if (scene != null)
			{
				scene.Update();
			}
		}

		public void LateUpdate()
		{
			if (scene != null)
			{
				scene.LateUpdate();
			}
		}

		private void OnCameraPosChange(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.OnCameraChange((float)evt.Parameters);
			}
		}

		private void OnChangeSceneByElevator(BaseEvent evt)
		{
			model.isElevatorTransfer = true;
			ChangeScene((SceneType)(int)evt.Parameters);
		}

		private void OnChangeSceneByAmbulance(BaseEvent evt)
		{
			model.isAmbulanceTransfer = true;
			ChangeScene((SceneType)(int)evt.Parameters);
		}

		private void OnChangeBrush(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.ChangeBrush((string)evt.Parameters);
			}
		}

		private void OnOpenCloseLamp(BaseEvent evt)
		{
			object[] array = (object[])evt.Parameters;
			if ((bool)array[1])
			{
				if (scene != null)
				{
					scene.OpenLamp((uint)array[0], (float)array[2]);
				}
			}
			else if (scene != null)
			{
				scene.CloseLamp((uint)array[0]);
			}
		}

		private void OnSunLightChange(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.ChangeSunLight((float)evt.Parameters);
			}
		}

		private void OnSunStateChange(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.ChangeSunState((bool)evt.Parameters);
			}
		}

		private void OnRemoveItem(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.RemoveItemByIndex((uint)evt.Parameters, true);
			}
		}

		private void OnCanTouchScene(BaseEvent evt)
		{
			if (scene != null)
			{
				scene.CanTouch((bool)evt.Parameters);
			}
		}

		private void OnCanTouchUI(BaseEvent evt)
		{
			if (gameView != null)
			{
				gameView.SetMaskActive(!(bool)evt.Parameters);
			}
		}

		public void DestroyLoadingView()
		{
			if (loadingView != null)
			{
				loadingView.Destroy();
				loadingView = null;
			}
			if (scene != null)
			{
				scene.CanTouch(true);
			}
		}

		public void ReleaseAllTouch()
		{
			if (scene != null)
			{
				scene.ReleaseAllTouch();
			}
		}

		public void ClearArchive()
		{
			if (model != null)
			{
				model.ResetArchiveData();
			}
		}

		public void SaveSceneArchive()
		{
			if (scene != null)
			{
				scene.SaveMapArchive();
			}
		}
	}
}
